package com.service.impl;

import com.dao.BrandDao;
import com.dao.ProductDao;
import com.dao.ProductTypeDao;
import com.dao.impl.BrandDaoImpl;
import com.dao.impl.ProductDaoImpl;
import com.dao.impl.ProductTypeDaoImpl;
import com.pojo.CarouselFigure;
import com.pojo.Classify;
import com.pojo.PageBean;
import com.pojo.Product;
import com.service.ProductService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class ProductServiceImpl implements ProductService {
    private ProductDao dao = new ProductDaoImpl();

    /**
     * 分页查询所有
     * @param limit
     * @param current
     * @return
     */
    @Override
    public PageBean<Product> findAll(int limit, int current) {
        int count = dao.selectCount();
        List<Product> list = dao.findAll((current - 1) * limit, limit);
        ProductTypeDao PTdao = new ProductTypeDaoImpl();
        BrandDao Bdao = new BrandDaoImpl();
        try {
            for (Product product : list) {
                //System.out.println(product.getCreateTime());
                //product.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(product.getCreateTime().toString()));
                product.setPt(PTdao.findByID(product.getProductType()));
                product.setBrand(Bdao.findByID(product.getProductBrand().trim()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        PageBean<Product> pageBean= new PageBean<>();
        pageBean.setData(list);
        pageBean.setCurrent(current);
        pageBean.setTotal(count);
        return pageBean;
    }

    @Override
    public Product findByID(String id) {
        Product product = dao.findByID(id);
        product.setPt(new ProductTypeDaoImpl().findByID(product.getProductType()));
        product.setBrand(new BrandDaoImpl().findByID(product.getProductBrand().trim()));
        return product;
    }

    @Override
    public boolean deleteP(String id) {
        return dao.deleteP(id)>0;
    }

    @Override
    public boolean addP(Product product) {
        return dao.addP(product)>0;
    }

    @Override
    public boolean update(Product product) {
        return dao.update(product)>0;
    }

    @Override
    public List<Product> newProduct() {
        return dao.newProduct();
    }

    @Override
    public List<Product> sortProduct(String productTypeName,String limit) {
        return dao.sortProduct(new ProductTypeDaoImpl().findByName(productTypeName).getId(),Integer.valueOf(limit));
    }

    @Override
    public List<Product> findProducts(Classify classify) {
        return dao.selectProducts(classify);
    }

    @Override
    public List<Product> findSimilarGoodsByID(String id) {
        return dao.selectSimilarGoodsByID(id);
    }
}
